﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WindowsAzure;

namespace AzureToolkit.Engine
{
    public class JobScheduler : IInitializer
    {
        private IAzureQueue<RunJobMessage> runJobQueue;

        public JobScheduler()
        {
            this.runJobQueue = new AzureQueue<RunJobMessage>();
        }

        public JobScheduler(CloudStorageAccount account)
        {
            this.runJobQueue = new AzureQueue<RunJobMessage>(account);
        }

        public JobScheduler(CloudStorageAccount account, string queueName)
        {
            this.runJobQueue = new AzureQueue<RunJobMessage>(account, queueName);
        }


        public void TriggerJob<TJob>() where TJob : IJob
        {
            TriggerJob(typeof(TJob));
        }

        public void TriggerJob(Type type)
        {
            TriggerJob(type.AssemblyQualifiedName);
        }

        private void TriggerJob(string jobName)
        {
            runJobQueue.AddMessage(new RunJobMessage
            {
                JobName = jobName,
            });
        }

        public void Initialize()
        {
            this.runJobQueue.Initialize();
        }
    }
}
